package com.yangfan.screenshot.utils;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Process;
import android.provider.Settings;

import com.yangfan.screenshot.log.LogUtil;

import java.lang.reflect.Method;

/**
 * @author : yangfan
 * @date : 2022/12/17-17:02
 * desc   : 检测手机品牌，做兼容性处理
 */
public final class DeviceUtils {
    private static final String TAG = DeviceUtils.class.getSimpleName();

    public static boolean isXiaomi() {
        return checkManufacturer("xiaomi");
    }

    public static boolean isOppo() {
        return checkManufacturer("oppo");
    }

    public static boolean isVivo() {
        return checkManufacturer("vivo");
    }

    /**
     * 是否允许后台弹出权限
     * @param context
     * @return
     */
    public static boolean isBackgroundStartAllowed(Context context){
        if (isXiaomi()) {
            return isXiaomiBgStartPermissionAllowed(context);
        }

        if (isVivo()) {
            return isVivoBgStartPermissionAllowed(context);
        }

        if (isOppo()) {
            return Settings.canDrawOverlays(context);
        }

        return true;
    }

    private static boolean isXiaomiBgStartPermissionAllowed(Context context) {
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            Method method = appOpsManager.getClass().getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
            int value = (int) method.invoke(appOpsManager, 10021, Process.myUid(), context.getPackageName());
            return value == AppOpsManager.MODE_ALLOWED;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean isVivoBgStartPermissionAllowed(Context context) {
        return getVivoBgStartPermissionStatus(context) == 0;
    }

    /**
     * 判断Vivo后台弹出界面状态， 1无权限，0有权限
     * @param context
     * @return
     */
    @SuppressLint("Range")
    private static Integer getVivoBgStartPermissionStatus(Context context) {
        Uri uri  = Uri.parse("content://com.vivo.permissionmanager.provider.permission/start_bg_activity");
        String selection = "pkgname = ?";
        String[] selectionArgs = new String[]{context.getPackageName()};
        int state = 1;
        try {
            Cursor cursor = context.getContentResolver().query(uri, null, selection, selectionArgs, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    state = cursor.getInt(cursor.getColumnIndex("currentstate"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return state;
    }

    private static boolean checkManufacturer(String manufacturer) {
        LogUtil.d(TAG,"Build.MANUFACTURER = " + Build.MANUFACTURER);
        return manufacturer.equals(Build.MANUFACTURER);
    }
}
